package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.pojos.article.ApArticle;
import com.heima.model.common.pojos.wemedia.WmNewsStatistics;
import com.heima.model.common.vo.WmNewsStatisticsVo;
import com.heima.wemedia.constant.WmConstants;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.mapper.WmNewsStatisticsMapper;
import com.heima.wemedia.receive.page.ResponseResult;
import com.heima.wemedia.service.WmNewsStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class WmNewsStatisticsServiceImpl extends ServiceImpl<WmNewsStatisticsMapper, WmNewsStatistics> implements WmNewsStatisticsService{
    @Autowired
    private  WmNewsStatisticsMapper mapper;
    @Autowired
    private WmNewsMapper wmNewsMapper;

    @Override
    public ResponseResult newsDimension(Date beginDate, Date endDate) {
        dataSynchronization();
        LambdaQueryWrapper<WmNewsStatistics> wrapper = Wrappers.<WmNewsStatistics>lambdaQuery();
        if(beginDate != null && endDate != null){
            wrapper.between(WmNewsStatistics::getCreatedTime,beginDate,endDate);
        }
        List<WmNewsStatistics> newsStatisticsList = mapper.selectList(wrapper);
        long likesNum = newsStatisticsList.stream().mapToLong(WmNewsStatistics::getLikes).sum();
        long collectNum = newsStatisticsList.stream().mapToLong(WmNewsStatistics::getCollection).sum();
        long forwardNum = newsStatisticsList.stream().mapToLong(WmNewsStatistics::getReadCount).sum();
        long publishNum = newsStatisticsList.size();
        Map<String, Long> map = new HashMap<>();
        map.put("likesNum",likesNum);
        map.put("collectNum",collectNum);
        map.put("forwardNum",forwardNum);
        map.put("publishNum",publishNum);
        return ResponseResult.okResult(map);
    }

    private void dataSynchronization() {
       List<ApArticle> apArticleList = mapper.selectApArticleList();
        for (ApArticle apArticle : apArticleList) {
            WmNewsStatistics statistics = mapper.selectOne(Wrappers.<WmNewsStatistics>lambdaQuery().eq(WmNewsStatistics::getArticle, apArticle.getId()));
            if(statistics == null){
                statistics = WmNewsStatistics.builder()
                        .article(apArticle.getId())
                        .createdTime(apArticle.getPublishTime())
                        .likes(apArticle.getLikes())
                        .collection(apArticle.getCollection())
                        .comment(apArticle.getComment())
                        .readCount(apArticle.getViews())
                        .build();
                mapper.insert(statistics);
            }else {
                WmNewsStatistics newStatistics = WmNewsStatistics.builder()
                        .id(statistics.getId())
                        .article(apArticle.getId())
                        .createdTime(apArticle.getPublishTime())
                        .likes(apArticle.getLikes())
                        .collection(apArticle.getCollection())
                        .comment(apArticle.getComment())
                        .readCount(apArticle.getViews())
                        .build();
                mapper.updateById(newStatistics);
            }
        }
    }

    @Override
    public ResponseResult newsPage(Date beginDate, Date endDate, Short page, Short size) {
        LambdaQueryWrapper<WmNewsStatistics> wrapper = Wrappers.<WmNewsStatistics>lambdaQuery();
        if(page == null || size == null){
            page = WmConstants.DEFAULT_PAGE;
            size = WmConstants.DEFAULT_SIZE;

        }
        int offset = (page - 1) * size;
        wrapper.orderByDesc(WmNewsStatistics::getCreatedTime);
        List<WmNewsStatistics> newsStatisticsList = mapper.selectItems(offset,size,beginDate,endDate);
        List<WmNewsStatisticsVo> statisticsVoList = newsStatisticsList.stream().map(statistics -> {
            WmNewsStatisticsVo statisticsVo = WmNewsStatisticsVo.builder()
                    .id(statistics.getId())
                    .userId(statistics.getUserId())
                    .article(statistics.getArticle())
                    .likes(statistics.getLikes())
                    .collection(statistics.getCollection())
                    .comment(statistics.getComment())
                    .views(statistics.getReadCount())
            .build();
            List<ApArticle> apArticleList = mapper.selectApArticleList();
            List<ApArticle> collect = apArticleList.stream().filter(apArticle -> {
                return Objects.equals(apArticle.getId(), statistics.getArticle());
            }).collect(Collectors.toList());
            statisticsVo.setTitle(collect.get(0).getTitle());
            return statisticsVo;
        }).collect(Collectors.toList());
        return ResponseResult.okResult(statisticsVoList);
    }
}
